package com.zhuyuan.auth.service.impl;

import com.wf.captcha.ArithmeticCaptcha;
import com.zhuyuan.auth.service.CaptchaService;
import com.zhuyuan.core.constant.Oauth2Constants;
import com.zhuyuan.core.exception.CaptchaException;
import com.zhuyuan.core.util.Result;
import com.zhuyuan.redis.RedisService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 张琳凯
 * @Description: TODO
 * @DateTime: 2025/2/19 10:40
 **/
@Service
@AllArgsConstructor
public class CaptchaServiceImpl implements CaptchaService {

    private final RedisService redisService;

    @Override
    public Result<?> getCaptchaCode() {
        Map<String, Object> data = new HashMap<>(2);
        String uuid = UUID.randomUUID().toString().replace("-","");
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(120, 40);
        captcha.getArithmeticString();  // 获取运算的公式：3+2=?
        // 获取运算的结果：5
        String text = captcha.text();
        redisService.setCacheObject(Oauth2Constants.REDIS_CAPTCHA_KEY + uuid, text, 2L, TimeUnit.MINUTES);
        data.put("uuid", uuid);
        data.put("img", captcha.toBase64());
        return Result.ok(data);
    }

    @Override
    public Result<?> getSmsCode(String mobile) {
        String code = "123";
        redisService.setCacheObject(Oauth2Constants.REDIS_SMS_KEY + mobile, code, 2L,TimeUnit.MINUTES);
        return Result.ok("发送成功");
    }

    @Override
    public void check(String key, String code) throws CaptchaException {

    }
}
